from backend.app.database.db_es import es_conn


class DeviceCRUD(object):
    client = None

    def __init__(self):
        #  获取连接
        if self.client is None:
            self.client = es_conn.get_client()

    def check_exists(self, index_name: str):
        client = self.client
        if not client.indices.exists(index=index_name):
            client.indices.create(index=index_name, body=device_mapping)

    def get_index(self, index_name: str):
        pass


# 索引数据结构
device_mapping = """
{
	"settings": {
		"number_of_shards": "1",
		"number_of_replicas": "2"
	},
	"mappings": {
		"properties": {
			"vec": {
				"type": "dense_vector",
				"dims": 1536
			},
			"g_id": {
				"type": "keyword"
			},
			"sid": {
				"type": "keyword"
			},
			"device_name": {
				"type": "keyword"
			},
			"picPath": {
				"type": "text",
				"index": false
			},
			"mod1": {
				"type": "keyword"
			},
			"mod": {
				"type": "keyword"
			},
			"brand_id": {
				"type": "keyword"
			},
			"brandTitle": {
				"type": "keyword"
			},
			"class_id": {
				"type": "keyword"
			},
			"class_name": {
				"type": "keyword"
			},
			"class_1_id": {
				"type": "keyword"
			},
			"class_1_name": {
				"type": "keyword"
			},
			"class_2_id": {
				"type": "keyword"
			},
			"class_2_name": {
				"type": "keyword"
			},
			"price": {
				"type": "text",
				"index": false
			},
			"meter": {
				"type": "text",
				"index": false
			},
			"meter_clean": {
				"type": "text",
				"index": false
			},
			"spare_meter": {
				"type": "text",
				"index": false
			},
			"address": {
				"type": "keyword"
			},
			"meter_coding": {
				"type": "keyword"
			},
			"countryTitle": {
				"type": "keyword"
			},
			"country": {
				"type": "keyword"
			}
		}
	}
}
"""
